<?php

namespace app\admin\controller\ykjp\information\basisinfo;

use app\common\controller\Backend;
use app\admin\model\ykjp\purchase\Order;
use app\admin\model\ykjp\purchase\Storage;
use app\admin\model\ykjp\purchase\Retire;

/**
 * 供应商管理
 *
 * @icon fa fa-circle-o
 */
class Supplier extends Backend
{
    
    /**
     * Supplier模型对象
     * @var \app\admin\model\ykjp\information\basisinfo\Supplier
     */
    protected $model = null;
    protected $distinguish=true;
    protected $wherename="firmid";
    protected $dataLimit = false;
    protected $dataLimitField = 'salesman_id';
   
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\ykjp\information\basisinfo\Supplier;
        $code="SP".date("YmdHis",time());
        $this->view->assign("code",$code);
        $authGoup=$this->auth->getGroupIds();
        $yjconfig=get_addon_config("ykjp");
        $this->view->assign("adminlimit",false);
        if(in_array(1,$authGoup)||$yjconfig['suinfo']==0){
            $this->dataLimit=false;  
        }else{
           if($yjconfig['suinfo']==1){
                $this->dataLimit="auth";
           }else{
               $this->dataLimit="personal";
           }
           $this->view->assign("adminlimit",true);  
        }
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $this->wherename="supplier.firmid";
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                     ->with(['admin'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();
            $list = $this->model
                    ->with(['admin'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                
                $row->getRelation('admin')->visible(['nickname']);
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     *获取业务员
     */
     public function getsalesman(){
        $this->dataLimit=false;
        $result=array("rows"=>[],"total"=>0);
        if ($this->request->isAjax()) {
           $this->model=model("admin");
           return parent::selectpage();
         }
         return json($result);
    }
    
    /**
     * 删除
     */

    public function del($ids=null){
      if($ids){
        $all=$this->model->where("id","in",$ids)->where("firmid",$this->auth->firmid)->select();
        $count=0;
        $this->model->startTrans();
        $Order=new Order;
        $Storage=new Storage;
        $Retire=new Retire;
        foreach ($all as $key => $val) {
           $id=$val["id"];
           $isOrder=$Order->where("supplier_id",$val['id'])->find();
           $isStorage=$Storage->where("supplier_id",$val['id'])->find();
           $isRetire=$Retire->where("supplier_id",$val['id'])->find();
            if($isStorage||$isStorage||$isRetire){
              $this->model->rollback();
              $this->error(__("Cant deltete"));
            }
           $up= $this->model->where("id",$val['id'])->update(array("deletetime"=>time()));
           $count+=$up;
        }
        if($count){
           $this->model->commit();
           $this->success();
        }else{
           $this->error();
        }

      }
      $this->error(__('Parameter %s can not be empty', 'ids'));
    }

}
